package com.bmadmin.controller.sys;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.LoginInfoDomain;
import com.bmadmin.domain.MessageDomain;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.service.SysUsersService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.bmadmin.util.Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.stuxuhai.jpinyin.PinyinException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by SEELE on 2017/12/3.
 */
@RestController
@RequestMapping("/users")
public class SysUserController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Autowired
    SysUsersService sysUsersService;

    /**
     * 用户登录验证
     */
    @PostMapping("/login")
    public NormalResult loginUser(HttpServletRequest request,HttpSession session, SysUsersPOJO sysUsersPOJO) {

        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            return new NormalResult("9999", "传入参数为空");
        }
        if ("".equals(Util.NulltoString(sysUsersPOJO.getU_id()))) {
            return new NormalResult("9999", "传入用户名为空");
        }
        if ("".equals(Util.NulltoString(sysUsersPOJO.getU_password()))) {
            return new NormalResult("9999", "传入密码为空");
        }
        result = sysUsersService.loginUser(session, sysUsersPOJO);
        logger.info(result);
        if(result.getCode().equals("0000")){
        	String ip = Util.getIPAddress(request);
        	logger.info("IP【"+ip+"】系统登陆成功");
        	LoginInfoDomain loginInfo = new LoginInfoDomain();
        	loginInfo.setS_date(BMUtil.getNowTimeForString());
        	loginInfo.setS_ip(ip);
        	loginInfo.setS_userid(sysUsersPOJO.getU_id());
        	loginInfo.setS_log("系统登陆");
        	sysUsersService.InsertSysLog(loginInfo);
        }
        return result;
    }


    /**
     * 获取当前用户信息
     */
    @PostMapping("/getNowLoginUser")
    public NormalResult loginUser(HttpSession session) {
        NormalResult result = new NormalResult();
        SysUsersPOJO sysUsersPOJO = (SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        if (!BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            result.setCode("0000");
            result.setMsg("用户已经登录");
            result.setObject(sysUsersPOJO);
        } else {
            result.setCode("9999");
            result.setMsg("用户未登录");
        }
        return result;
    }

    /**
     * 用户退出
     */
    @PostMapping("/safeQuit")
    public NormalResult safeQuit(HttpSession session) {
        session.removeAttribute(Constants.USER_INFO_SESSION);
        return new NormalResult("0000", "退出成功");
    }

    /**
     * 用户修改密码
     */
    @PostMapping("/resetPassword")
    public NormalResult resetPassword(HttpSession session, SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            return new NormalResult("9999", "传入参数为空");
        }
        if ("".equals(Util.NulltoString(sysUsersPOJO.getU_id()))) {
            return new NormalResult("9999", "传入用户名为空");
        }
        if ("".equals(Util.NulltoString(sysUsersPOJO.getU_password()))) {
            return new NormalResult("9999", "传入新密码为空");
        }
        result = sysUsersService.resetPassword(sysUsersPOJO);

        return result;
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/getUsers")
    public LayUiDataTablePOJO getUserList(SysUsersPOJO sysUsersPOJO) {
        LayUiDataTablePOJO result = sysUsersService.getUsers(sysUsersPOJO);
        logger.info(result);
        return result;
    }

    /**
     * 用户新增
     */
    @PostMapping("/insert")
    public NormalResult insertUser(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

        if (BMUtil.isNullOrEmpty(sysUsersPOJO.getU_id()) || BMUtil.isNullOrEmpty(sysUsersPOJO.getU_name()) || BMUtil.isNullOrEmpty(sysUsersPOJO.getD_id())) {
            result.setCode("9999");
            result.setMsg("部分参数为空，请检查用户id，用户名称");
            return result;
        }
        try {
            result = sysUsersService.insertUser(sysUsersPOJO);
        } catch (PinyinException e) {
            result.setCode("9999");
            result.setMsg("用户拼音转换失败");
            logger.error(e.toString());
        }
        return result;
    }

    /**
     * 用户删除
     */
    @PostMapping("/delete")
    public NormalResult deleteUserById(SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysUsersPOJO)){
            result.setCode("9999");
            result.setMsg("用户删除传入数据为空!");
            return result;
        }

        result = sysUsersService.deleteUser(sysUsersPOJO);
        return result;
    }
    /**
     * 用户修改
     */
    @PostMapping("/update")
    public NormalResult updateUser(SysUsersPOJO sysUsersPOJO){
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

        if (BMUtil.isNullOrEmpty(sysUsersPOJO.getU_id()) || BMUtil.isNullOrEmpty(sysUsersPOJO.getU_name()) || BMUtil.isNullOrEmpty(sysUsersPOJO.getD_id())) {
            result.setCode("9999");
            result.setMsg("部分参数为空，请检查用户id，用户名称");
            return result;
        }

        result = sysUsersService.updateUser(sysUsersPOJO);
        return result;
    }

    @GetMapping("/getByIdTakeDepartmentAndRoles")
    public NormalResult getByIdTakeDepartmentAndRoles(SysUsersPOJO sysUsersPOJO){
        NormalResult result = new NormalResult();
        result=sysUsersService.getByIdTakeDepartmentAndRoles(sysUsersPOJO);

        return result;
    }
    
    /**
     * 获取系统操作日志
     */
	@GetMapping("/getLogs")
    public Object getLogs(HttpSession session,LoginInfoDomain InfoDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
		map.put("qs_date", InfoDomain.getQs_date());
		map.put("jz_date", InfoDomain.getJz_date());
		map.put("s_userid", InfoDomain.getS_userid());
		map.put("s_ip",InfoDomain.getS_ip());
		PageHelper.startPage(page, limit);
		List<LoginInfoDomain> list = sysUsersService.getLogList(map);
		PageInfo pagelist = new PageInfo(list);
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }

    /**
     * 用户密码重置
     */
    @PostMapping("/reset")
    public NormalResult reset(HttpSession session, SysUsersPOJO sysUsersPOJO) {
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sysUsersPOJO)) {
            return new NormalResult("9999", "传入参数为空");
        }
        if ("".equals(Util.NulltoString(sysUsersPOJO.getU_id()))) {
            return new NormalResult("9999", "传入用户名为空");
        }
        sysUsersPOJO.setU_password("Abcd1234");//初始密码Abcd1234
        result = sysUsersService.resetPassword(sysUsersPOJO);

        return result;
    }

}
